package com.yiji.fintech.musfinance.common.strategy.unique;

import com.acooly.core.utils.Collections3;
import com.yiji.fintech.musfinance.base.enums.ElementTypeEnum;
import com.yiji.fintech.musfinance.base.enums.TransStatusEnum;
import com.yiji.fintech.musfinance.center.base.result.ResultFacade;
import com.yiji.fintech.musfinance.center.sign.result.SignResult;
import com.yiji.fintech.musfinance.common.domain.SignDomain;
import com.yiji.fintech.musfinance.common.domain.base.BaseDomain;
import com.yiji.fintech.musfinance.common.exception.UniqueCheckException;
import com.yiji.fintech.musfinance.common.service.SignUtilService;
import com.yiji.fintech.musfinance.common.strategy.base.AbstractUniqueStrategy;
import com.yiji.fintech.musfinance.common.utils.TransStatusConverter;
import com.yiji.fintech.musfinance.entity.SignContractno;
import com.yiji.fintech.musfinance.entity.SignInfo;
import com.yiji.fintech.musfinance.entity.AbstractTransaction;
import com.yiji.fintech.musfinance.service.SignContractnoService;
import com.yiji.fintech.musfinance.service.SignInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 签约唯一性校验
 * Created by ouwen@yiji.com} on 2017/9/27.
 */
@Service
public class SignUniqueStrategy extends AbstractUniqueStrategy {
    @Autowired
    private SignInfoService signInfoService;
    @Autowired
    private SignUtilService signUtilService;
    @Autowired
    private SignContractnoService signContractnoService;

    @Override
    public void process(List<AbstractTransaction> datas, BaseDomain domain, ResultFacade result) {
        SignDomain signDomain = (SignDomain)domain;
        SignResult signResult = (SignResult)result;
        if(Collections3.isNotEmpty(datas)){//根据流水号查唯一性
            AbstractTransaction transaction = datas.get(0);
            TransStatusConverter.converter(transaction.getTransStatus(),result);//交易结果转换
            //获取交易协议信息
            setContractNo(signDomain,signResult,transaction.getTransStatus());
            signResult.setDetail("商户已签约【"+signDomain.getElementType().getMessage()+"】");
            throw new UniqueCheckException("订单唯一性校验不通过流水【"+transaction.getBizOrderNo()+"】存在");
        }
        //根据商户要素查询是否存在签约信息
        SignInfo signInfo = signUtilService.isExsit(signDomain);
        if(signInfo != null){
            TransStatusConverter.converter(TransStatusEnum.BANK_SUCCESS,result);//交易结果转换
            signResult.setDetail("商户已签约【"+signInfo.getElementType().getMessage()+"】");
            setContractNo(signDomain,signResult,TransStatusEnum.BANK_SUCCESS);
            throw new UniqueCheckException("订单【"+signDomain.getMerchOrderNo()+"】签约信息存在");
        }
    }
    public void setContractNo(SignDomain signDomain,SignResult signResult,TransStatusEnum transStatus){
        ElementTypeEnum elementTypeEnum = signDomain.getElementType();
        if(elementTypeEnum == ElementTypeEnum.FOUR && transStatus == TransStatusEnum.BANK_SUCCESS){//需要查询渠道协议表
            SignInfo signInfo = new SignInfo();
            signInfo.setElementType(elementTypeEnum);
            signInfo.setAccountName(signDomain.getAccountName());
            signInfo.setCertNo(signDomain.getCertNo());
            signInfo.setPhoneNo(signDomain.getPhoneNo());
            signInfo.setAccountNo(signDomain.getAccountNo());
            SignInfo signInfoDb = signInfoService.query(signInfo);
            SignContractno signContractno = new SignContractno();
            signContractno.setSignInfoId(signInfoDb.getSignInfoId());
            signContractno.setGatewayChannelApi(signDomain.getGatewayChannelApi());
            SignContractno signContractnoDb = signContractnoService.query(signContractno);
            signResult.setContractNo(signContractnoDb.getContractNo());
        }
    }
}
